// ----------------------------------------------------------------------------------
// Microsoft Developer & Platform Evangelism
// 
// Copyright (c) Microsoft Corporation. All rights reserved.
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
// ----------------------------------------------------------------------------------

#region File Description
//-----------------------------------------------------------------------------
// MainMenuScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
#endregion

namespace HexTD
{
	class MainMenuScreen : MenuScreen
	{
		Dictionary<MenuEntry, string[]> menuToMap = new Dictionary<MenuEntry, string[]>();

		#region Initialization
		public MainMenuScreen()
			: base(String.Empty)
		{
			IsPopup = true;

			AddMapItems();

			// Create our menu entries.
			MenuEntry exitMenuEntry = new MenuEntry("Exit");

			// Hook up menu event handlers.
			exitMenuEntry.Selected += OnCancel;

			// Add entries to the menu.
			MenuEntries.Add(exitMenuEntry);
		}

		void AddMapItems()
		{
			menuToMap.Clear();
			List<string[]> mapNames = MapIO.loadIndex();

			foreach (string[] mapInfo in mapNames)
			{
				// Create our menu entries.
				MenuEntry mapMenuEntry = new MenuEntry(mapInfo[0]);
				// Link this menu entry to map info.
				menuToMap[mapMenuEntry] = mapInfo;
				// Hook up menu event handlers.
				mapMenuEntry.Selected += StartGameMenuEntrySelected;
				// Add entries to the menu.
				MenuEntries.Add(mapMenuEntry);
			}
		}
		#endregion

		#region Overrides
		protected override void UpdateMenuEntryLocations()
		{
			base.UpdateMenuEntryLocations();

			foreach (var entry in MenuEntries)
			{
				var position = entry.Position;

				position.Y += 60;

				entry.Position = position;
			}
		}
		#endregion

		#region Event Handlers for Menu Items
		/// <summary>
		/// Handles "Play" menu item selection
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		void StartGameMenuEntrySelected(object sender, EventArgs e)
		{
			InstructionsScreen instructionScreen = new InstructionsScreen();
			ScreenManager.AddScreen(instructionScreen, null);
			string[] mapInfo = menuToMap[(MenuEntry)sender];
			string mapUrl = mapInfo[1];
			instructionScreen.ShowGameplayScreen(mapUrl);
		}

		/// <summary>
		/// Handles "Exit" menu item selection
		/// </summary>
		/// 
		protected override void OnCancel(PlayerIndex playerIndex)
		{
			ScreenManager.Game.Exit();
		}
		#endregion
	}
}
